var MainTagSpecialTags = [
    { TagName: "Animal Welfare", SpecialTag: null, EventIndicator: "Animal Welfare" },
    { TagName: "Animal Welfare", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Anti-competitive Practices", SpecialTag: null, EventIndicator: "Anti-Competitive Practices" },
    { TagName: "Anti-competitive Practices", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Arms Export", SpecialTag: null, EventIndicator: "Weapons" },
    { TagName: "Arms Export", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Board composition", SpecialTag: null, EventIndicator: "Corporate Governance" },
    { TagName: "Board composition", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Bribery and Corruption", SpecialTag: null, EventIndicator: "Bribery and Corruption" },
    { TagName: "Bribery and Corruption", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Business Ethics - Other", SpecialTag: null, EventIndicator: "Business Ethics" },
    { TagName: "Business Ethics - Other", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Child Labour", SpecialTag: null, EventIndicator: "Child Labour" },
    { TagName: "Child Labour", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Conflicts with Indigenous Communities", SpecialTag: null, EventIndicator: "Community Relations" },
    { TagName: "Conflicts with Indigenous Communities", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Conflicts with Local Communities", SpecialTag: null, EventIndicator: "Community Relations" },
    { TagName: "Conflicts with Local Communities", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Controversial Weapons", SpecialTag: null, EventIndicator: "Weapons" },
    { TagName: "Controversial Weapons", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Corporate Governance - other", SpecialTag: null, EventIndicator: "Corporate Governance" },
    { TagName: "Corporate Governance - other", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Customer - Other", SpecialTag: null, EventIndicator: "Quality and Safety" },
    { TagName: "Customer - Other", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Customer Management", SpecialTag: null, EventIndicator: "Quality and Safety" },
    { TagName: "Customer Management", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Degradation & Contamination (Land)", SpecialTag: null, EventIndicator: "Emissions, Effluents and Waste" },
    { TagName: "Degradation & Contamination (Land)", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Discharges and Releases (Water)", SpecialTag: null, EventIndicator: "Emissions, Effluents and Waste" },
    { TagName: "Discharges and Releases (Water)", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Discrimination & Harassment", SpecialTag: null, EventIndicator: "Basic Labour Standards" },
    { TagName: "Discrimination & Harassment", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Emissions to Air", SpecialTag: null, EventIndicator: "Emissions, Effluents and Waste" },
    { TagName: "Emissions to Air", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Employees - Other", SpecialTag: null, EventIndicator: "Labour Relations" },
    { TagName: "Employees - Other", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Energy Use and Greenhouse Gas Emissions", SpecialTag: null, EventIndicator: "Energy Use and GHG Emissions" },
    { TagName: "Energy Use and Greenhouse Gas Emissions", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Environment - Other", SpecialTag: null, EventIndicator: "Conservation and Land Use" },
    { TagName: "Environment - Other", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Environmental impact of products", SpecialTag: null, EventIndicator: "Environmental Impact of Products" },
    { TagName: "Environmental impact of products", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "False or Deceptive Marketing", SpecialTag: null, EventIndicator: "Marketing Practices" },
    { TagName: "False or Deceptive Marketing", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Forced Labour", SpecialTag: null, EventIndicator: "Forced labour" },
    { TagName: "Forced Labour", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Fraud", SpecialTag: null, EventIndicator: "Accounting and Taxation" },
    { TagName: "Fraud", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Freedom of Association", SpecialTag: null, EventIndicator: "Basic Labour Standards" },
    { TagName: "Freedom of Association", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Health and Safety", SpecialTag: null, EventIndicator: "Health and Safety" },
    { TagName: "Health and Safety", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Insider Trading", SpecialTag: null, EventIndicator: "Business Ethics" },
    { TagName: "Insider Trading", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Intellectual Property", SpecialTag: null, EventIndicator: "Intellectual Property" },
    { TagName: "Intellectual Property", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Involvement With Entities Violating Human Rights", SpecialTag: null, EventIndicator: "Complicity in Human Rights Violations" },
    { TagName: "Involvement With Entities Violating Human Rights", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Labour Relations", SpecialTag: null, EventIndicator: "Labour Relations" },
    { TagName: "Labour Relations", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Land Rights", SpecialTag: null, EventIndicator: "Community Relations" },
    { TagName: "Land Rights", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Land Use and Biodiversity", SpecialTag: null, EventIndicator: "Conservation and Land Use" },
    { TagName: "Land Use and Biodiversity", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" },
    { TagName: "Lobbying and Public Policy", SpecialTag: null, EventIndicator: "Lobbying and Public Policy" },
    { TagName: "Lobbying and Public Policy", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Local Community - Other", SpecialTag: null, EventIndicator: "Community Relations" },
    { TagName: "Local Community - Other", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Occupied Territories/Disputed Regions", SpecialTag: null, EventIndicator: "Complicity in Human Rights Violations" },
    { TagName: "Occupied Territories/Disputed Regions", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Other Labour Standards", SpecialTag: null, EventIndicator: "Basic Labour Standards" },
    { TagName: "Other Labour Standards", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Privacy", SpecialTag: null, EventIndicator: "Privacy" },
    { TagName: "Privacy", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Product Quality and Safety", SpecialTag: null, EventIndicator: "Quality and Safety" },
    { TagName: "Product Quality and Safety", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Remuneration", SpecialTag: null, EventIndicator: "Corporate Governance" },
    { TagName: "Remuneration", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Resilience", SpecialTag: null, EventIndicator: "Resilience" },
    { TagName: "Resilience", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Sanctions", SpecialTag: null, EventIndicator: "Sanctions" },
    { TagName: "Sanctions", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Services Quality and Safety", SpecialTag: null, EventIndicator: "Quality and Safety" },
    { TagName: "Services Quality and Safety", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Shareholder Disputes/Rights", SpecialTag: null, EventIndicator: "Corporate Governance" },
    { TagName: "Shareholder Disputes/Rights", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Social impact of products", SpecialTag: null, EventIndicator: "Social Impact of Products" },
    { TagName: "Social impact of products", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Taxes avoidance/evasion", SpecialTag: null, EventIndicator: "Accounting and Taxation" },
    { TagName: "Taxes avoidance/evasion", SpecialTag: "Supply Chain", EventIndicator: "Governance - Supply Chain" },
    { TagName: "Water Rights", SpecialTag: null, EventIndicator: "Community Relations" },
    { TagName: "Water Rights", SpecialTag: "Supply Chain", EventIndicator: "Social - Supply Chain" },
    { TagName: "Water Use", SpecialTag: null, EventIndicator: "Water Use" },
    { TagName: "Water Use", SpecialTag: "Supply Chain", EventIndicator: "Environment - Supply Chain" }
];

function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;
    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }
    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";
    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);
    return new BinData(3, base64);
}

use ClientPlatform

if (!db.CriteriaLevel2.findOne({ "Name" : "Incidents" })) {
    db.CriteriaLevel2.insert({
        "_id": NUUID(uuid()),
        "CriteriaLevel1Id" : db.CriteriaLevel1.findOne({ "Name": "ESG Performance" })._id,
        "Name" : "Incidents",
        "UniverseFeature" : 8,
        "ShortName" : "EsgIncidents",
        "Order" : 7
    });
}

var CriteriaLevel2Id = db.CriteriaLevel2.findOne({ "Name" : "Incidents" })._id;

var i = 0;
MainTagSpecialTags.forEach(function (item) {
    i++;
    
    if (!db.CriteriaLevel3.findOne({ "Name" : item.TagName, "Category": item.EventIndicator })) {
        db.CriteriaLevel3.insert({
            "_id" : NUUID(uuid()),
            "Name" : item.TagName,
            "ShortName" : "",
            "Order" : i,
            "CriteriaLevel2Id" : CriteriaLevel2Id,
            "Category" : item.EventIndicator
        });
    }
    
    var CriteriaLevel3Id = db.CriteriaLevel3.findOne({ "Name" : item.TagName, "Category": item.EventIndicator })._id;
    
    var oldTemplateCriteriaName = [item.EventIndicator, item.TagName].join(" - ");
    if (!db.OldTemplateCriteria.findOne({ "Name" : oldTemplateCriteriaName, "CriteriaLevel2Id": CriteriaLevel2Id })) {
        db.OldTemplateCriteria.insert({
            "_id" : NUUID(uuid()),
            "Name" : oldTemplateCriteriaName,
            "CriteriaEntity" : "ScreeningData",
            "CriteriaEntityField" : oldTemplateCriteriaName,
            "Score" : null,
            "TemplateIndicatorNumber" : null,
            "CriteriaLevel2Id" : CriteriaLevel2Id,
            "CriteriaType" : 9,
            "FilterId" : null,
            "CompaniesIdsResult" : null,
            "Order" : i
        });
    }

    ["Answer Category", "Impact Answer Category", "Risk Answer Category"].forEach(function (filterText) {
        var templateCriteriaName = [item.EventIndicator, item.TagName, filterText].join(" - ");
        if (!db.TemplateCriteria.findOne({ "Name" : templateCriteriaName, "CriteriaLevel2Id": CriteriaLevel2Id })) {
            db.TemplateCriteria.insert({
                "_id" : NUUID(uuid()),
                "Name" : templateCriteriaName,
                "CriteriaEntity" : "ScreeningData",
                "CriteriaEntityField" : templateCriteriaName,
                "CriteriaType" : 9,
                "CriteriaLevel2Id" : CriteriaLevel2Id,
                "CriteriaLevel3Id" : CriteriaLevel3Id,
                "IsScreenable" : filterText === "Answer Category"
            });
        }
    });
});